home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1994 / MacHack 1994.toast / MacHack™ 1987-1994 / MacHack™ '90 / MacHack'90 Proceedings / John Norstad / Reusable Code / Source / wrap.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-06-10  |  2.9 KB  |  122 lines  |  [TEXT/MPS ]

  1. /*______________________________________________________________________
  2.  
  3.     wrap.c - Tool to word wrap paragraphs.
  4.     
  5.     Copyright © 1988, 1989, 1990 Northwestern University.  Permission is 
  6.     granted to use this code in your own projects, provided you give credit 
  7.     to both John Norstad and Northwestern University in your about box or 
  8.     document.
  9.     
  10.     This tool reads standard input and writes standard output.  It
  11.     word wraps each paragraph from the input file.
  12.     
  13.     In Disinfectant I use this tool to help build the online document.  I
  14.     maintain the source using Microsoft word on a text-only file, without line 
  15.     breaks.  This saves each paragraph as one big long line.  My makefile 
  16.     runs the wrap tool to word wrap each of these paragraphs to the exact 
  17.     width of my help window rectangle.  The output of wrap is then fed through 
  18.     the cvrt tool to generate the sequence of STR# resources used by the program.
  19.     
  20.     wrap -r xxxx [-p]
  21.     
  22.     xxxx = right margin in pixels.
  23.     
  24.     -p = if specified, add a special end-of-paragraph byte 31 to the
  25.         end of each line.  This option should be specified when preparing
  26.         type 1 reports for the rep.c module.  Rep.c uses the special eop
  27.         markers to identify paragraphs in the printing code.
  28.     
  29. _____________________________________________________________________*/
  30.  
  31. #pragma load "precompile"
  32.  
  33. #include "doc.h"
  34.  
  35.  
  36. short main(short argc, char *argv[])
  37.  
  38. {
  39.     char        line[10000];
  40.     char        *first;
  41.     char        *last;
  42.     char        *prev;
  43.     char        *next;
  44.     char        save;
  45.     short        len;
  46.     GrafPort    myPort;
  47.     Boolean    rSpecified;
  48.     Boolean    pSpecified;
  49.     short        margin;
  50.     short        i;
  51.     
  52.     /* Crack and check parameters. */
  53.  
  54.     i = 1;
  55.     rSpecified = pSpecified = false;
  56.     while (i < argc) {
  57.         if (*argv[i] == '-') {
  58.             if (tolower(*(argv[i]+1)) == 'r') {
  59.                 rSpecified = true;
  60.                 margin = atoi(argv[i+1]);
  61.                 i += 2;
  62.             } else if (tolower(*(argv[i]+1)) == 'p') {
  63.                 pSpecified = true;
  64.                 i += 2;
  65.             } else {
  66.                 fprintf(stderr, "### %s - Usage: %s -r rightmargin [-p].\n", 
  67.                     argv[0], argv[0]);
  68.                 return 1;
  69.             };
  70.         } else {
  71.             fprintf(stderr, "### %s - Usage: %s -r rightmargin [-p].\n", 
  72.                 argv[0], argv[0]);
  73.             return 1;
  74.         };
  75.     };
  76.     if (!rSpecified) {
  77.         fprintf(stderr, "### %s - right margin not specified.\n", argv[0]);
  78.         return 1;
  79.     };
  80.  
  81.     /* Process file. */
  82.     
  83.     InitGraf(&qd.thePort);
  84.     OpenPort(&myPort);
  85.     TextFont(applFont);
  86.     TextSize(9);
  87.     while (gets(line)) {
  88.         first = last = prev = line;
  89.         len = strlen(line);
  90.         *(line+len) = ' ';
  91.         *(line+len+1) = 0;
  92.         while (true) {
  93.             while (true) {
  94.                 next = strchr(prev, ' ');
  95.                 if (!next || TextWidth(first, 0, next-first) > margin ) break;
  96.                 last = next;
  97.                 prev = last + strspn(last, " ");
  98.             };
  99.             save = *last;
  100.             if (next) {
  101.                 *last = 0;
  102.                 puts(first);
  103.                 *last = save;
  104.                 first = prev;
  105.                 last = next;
  106.             } else {
  107.                 if (pSpecified) {
  108.                     *last = docEop;
  109.                     *(last+1) = 0;
  110.                     puts(first);
  111.                     break;
  112.                 } else {
  113.                     *last = 0;
  114.                     puts(first);
  115.                     break;
  116.                 };
  117.             };
  118.         };
  119.     };
  120.     
  121.     return 0;
  122. }